//
//  CourseEvaluateAudioView.m
//  优学荟管理系统
//
//  Created by 星道 on 17/7/14.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "CourseEvaluateAudioView.h"
#import "EMCDDeviceManager.h"

@implementation CourseEvaluateAudioView
{
    IBOutlet UIView *_BackView;
    IBOutlet UIImageView *_CloseImage;
    void (^_ClickCommit)(BOOL);
}

/**
 实例化
 */
+ (instancetype)ConsumeSureWithClickCommit:(void(^)(BOOL IsSelect))ClickCommit
{
    return [[self alloc] initWithClickCommit:ClickCommit];
}
//初始化
- (instancetype)initWithClickCommit:(void (^)(BOOL IsSelect))ClickCommit
{
    self = [[[NSBundle mainBundle] loadNibNamed:[CourseEvaluateAudioView description] owner:nil options:nil] lastObject];
    if (self) {

        [[UIApplication sharedApplication].keyWindow addSubview:self];

        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT);

        _ClickCommit = ClickCommit;

        //textfield加上阴影效果
        [[_BackView layer] setMasksToBounds:YES];
        [[_BackView layer] setShadowOffset:CGSizeMake(1, 1)]; // 阴影的范围
        [[_BackView layer] setShadowRadius:3];                // 阴影扩散的范围控制
        [[_BackView layer] setShadowOpacity:1];               // 阴影透明度
        [[_BackView layer] setShadowColor:[UIColor blackColor].CGColor]; // 阴影的颜色
        [_BackView setClipsToBounds:NO];

        //添加关闭手势
        [_CloseImage addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Close)]];

        self.alpha = 0;

        //弹出窗口
        [self Show];
    }
    return self;
}




/**
 显示自己
 */
- (void)Show {

    //动画展示
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
    }];
}

- (void)Close {
    [self Close:YES];
}

/**
 关闭
 */
- (void)Close:(BOOL)IsCancle
{
    IsCancle ? [[EMCDDeviceManager sharedInstance] cancelCurrentRecording]:nil;
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


/**
 开始录音
 */
- (IBAction)ClickAudio:(UIButton *)sender
{
    sender.selected = !sender.selected;


    sender.userInteractionEnabled = NO;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        sender.userInteractionEnabled = YES;
    });

    if (_ClickCommit)
    {
        _ClickCommit (sender.selected);
    }

    sender.selected == NO ? [self Close:NO]:nil;
}

@end
